﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

using System.ComponentModel;

using System.Data.Linq;
using System.Data.Linq.Mapping;

using System.Linq.Expressions;
using System.Reflection;


public partial class ProductsList : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
          // Retrieve DepartmentID from the query string
        string departmentId = Request.QueryString["DepartmentID"];
        // Retrieve CategoryID from the query string
        string categoryId = Request.QueryString["CategoryID"];
        // Retrieve Page from the query string
        string page = Request.QueryString["Page"];
        if (page == null) page = "1";
        // How many pages of products?
        int howManyPages = 1;
        // pager links format
        string firstPageUrl = "";
        string pagerFormat = "";

        // If browsing a category...
        string search = Request.QueryString["search"];
        // If browsing a category...
        if (search != null)
        {
            list.DataSource =    CatalogAccess.search(search);
            list.DataBind();
        }
        else
        if (categoryId != null)
        {
            // Retrieve list of products in a category
            list.DataSource =
            CatalogAccess.GetProductsInCategory(categoryId, page, out howManyPages);
            list.DataBind();
            // get first page url and pager format
            firstPageUrl = Link.ToCategory(departmentId, categoryId, "1");
            pagerFormat = Link.ToCategory(departmentId, categoryId, "{0}");
            // Display pager controls
            topPager.Show(int.Parse(page), howManyPages, firstPageUrl, pagerFormat, false);
            bottomPager.Show(int.Parse(page), howManyPages, firstPageUrl, pagerFormat, true);
        }
        else if (departmentId != null)
        {
            // Retrieve list of products on department promotion
            list.DataSource = CatalogAccess.GetProductsOnDeptPromo
            (departmentId, page, out howManyPages);
            list.DataBind();
            // get first page url and pager format
            firstPageUrl = Link.ToDepartment(departmentId, "1");
            pagerFormat = Link.ToDepartment(departmentId, "{0}");
            // Display pager controls
            topPager.Show(int.Parse(page), howManyPages, firstPageUrl, pagerFormat, false);
            bottomPager.Show(int.Parse(page), howManyPages, firstPageUrl, pagerFormat, true);
        }
        else
        {
            // Retrieve list of products on catalog promotion
            list.DataSource =
            CatalogAccess.GetProductsOnFrontPromo(page, out howManyPages);
            list.DataBind();
            // have the current page as integer
            int currentPage = Int32.Parse(page);
            // Display pager controls
            topPager.Show(int.Parse(page), howManyPages, firstPageUrl, pagerFormat, false);
            bottomPager.Show(int.Parse(page), howManyPages, firstPageUrl, pagerFormat, true);
        }

     
    }
    protected void list_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        string departmentId = Request.QueryString["DepartmentID"];
        // Retrieve CategoryID from the query string
        string categoryId = Request.QueryString["CategoryID"];
        // Retrieve Page from the query string
        string productId = "";
        string search = Request.QueryString["search"];
        if (search != null)
        {
            StoredProcedure1Result dataRow = (StoredProcedure1Result)e.Item.DataItem;
            productId = dataRow.ProductID.ToString();
        }
        else if (categoryId != null)
        {
            CatalogGetProductsInCategoryResult dataRow = (CatalogGetProductsInCategoryResult)e.Item.DataItem;
            productId = dataRow.ProductID.ToString();
        }
        else if (departmentId != null)
        {
            CatalogGetProductsOnDeptPromoResult dataRow = (CatalogGetProductsOnDeptPromoResult)e.Item.DataItem;
            productId = dataRow.ProductID.ToString();
        }
        else
        {
            CatalogGetProductsOnFrontPromoResult dataRow = (CatalogGetProductsOnFrontPromoResult)e.Item.DataItem;
            productId = dataRow.ProductID.ToString();
        }
       
        ISingleResult<CatalogGetProductAttributeValuesResult>  attrTable = CatalogAccess.GetProductAttributes(productId);
   PlaceHolder attrPlaceHolder = (PlaceHolder)e.Item.FindControl("attrPlaceHolder");

        // temp variables
        string prevAttributeName = "";
        string attributeName, attributeValue, attributeValueId;

        // current DropDown for attribute values
        Label attributeNameLabel;
        DropDownList attributeValuesDropDown = new DropDownList();

        // read the list of attributes
        foreach (CatalogGetProductAttributeValuesResult r in attrTable)
        {
            // get attribute data
            attributeName = r.AttributeName.ToString();
            attributeValue = r.AttributeValue.ToString();
            attributeValueId = r.AttributeValueID.ToString();

            // if starting a new attribute (e.g. Color, Size)
            if (attributeName != prevAttributeName)
            {
                prevAttributeName = attributeName;
                attributeNameLabel = new Label();
                attributeNameLabel.Text = attributeName + ": ";
                attributeValuesDropDown = new DropDownList();
                attrPlaceHolder.Controls.Add(attributeNameLabel);
                attrPlaceHolder.Controls.Add(attributeValuesDropDown);
            }

            // add a new attribute value to the DropDownList
            attributeValuesDropDown.Items.Add(new ListItem(attributeValue, attributeValueId));
        }
    }
    
    
    
    
    
    
    
}
